package com.firefighting.util;


import com.itextpdf.text.BaseColor;
import com.itextpdf.text.ExceptionConverter;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPCellEvent;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.RadioCheckField;
/**
 * PCell Event
 * @author donald
 *
 */
public class CheckboxCellEvent implements PdfPCellEvent{
	 // The name of the check box field
    protected String name;
    protected boolean flag ;
    private Integer hookSize = 5;
    private Integer edgeSize = 10;
    // We create a cell event
    public CheckboxCellEvent(String name, boolean flag) {
        this.name = name;
        this.flag = flag;
    }
    public CheckboxCellEvent(String name, boolean flag , Integer hookSize, Integer edgeSize ) {
        this.name = name;
        this.flag = flag;
        this.hookSize = hookSize;
        this.edgeSize = edgeSize;
    }
    // We create and add the check box field
    public void cellLayout(PdfPCell cell, Rectangle position,
        PdfContentByte[] canvases) {
        PdfWriter writer = canvases[0].getPdfWriter(); 
        // define the coordinates of the middle
        float x = (position.getLeft() + position.getRight()) / 2;
        float y = (position.getTop() + position.getBottom()) / 2;
        // define the position of a check box that measures 20 by 20
        //画勾
        Rectangle rect = new Rectangle(x - hookSize, y - hookSize, x + hookSize, y + hookSize);
        RadioCheckField checkbox = new RadioCheckField(writer, rect, name, "On");
        checkbox.setCheckType(RadioCheckField.TYPE_CHECK);
        
        if(flag){
        	//设置为选中状态
        	checkbox.setChecked(true);
        }
        else{
        	checkbox.setChecked(false);
        }
        //画框
        PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
        canvas.setColorStroke(BaseColor.BLACK);
        canvas.setLineDash(1f);
        canvas.rectangle(x - edgeSize, y - edgeSize, edgeSize+edgeSize,edgeSize+edgeSize);
        canvas.stroke();
       
        try {
            writer.addAnnotation(checkbox.getCheckField());
        } catch (Exception e) {
            throw new ExceptionConverter(e);
        }
    }
}